<%--
  Created by IntelliJ IDEA.
  User: 22541
  Date: 2022/4/6
  Time: 19:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>课程</title>
</head>
<body>
<div align="center">
    <h2>课程总览</h2>
    你好,${user.username}同学
</div>
<br>
<table border="1" cellspacing="0" width="80%" align="center">
    <tr>
        <th></th>
        <th id="cId" >序号</th>
        <th id="cName">课程名称
            <c:if test="${type == 1}">&darr;</c:if>
            <c:if test="${type == 2}">&uarr;</c:if>
        </th>
        <th id = "cNum">选课人数
            <c:if test="${type == 3}">&darr;</c:if>
            <c:if test="${type == 4}">&uarr;</c:if>
        </th>
        <th>课程性质</th>
    </tr>

    <c:forEach items="${page.list}" var="course" varStatus="status">
        <tr align="center">
            <td><input type="checkbox" class="cb" id="${course.id}" name="checkbox" value="${course.id}"></td>
            <td>${course.id}</td>
            <td>${course.name}</td>
            <td>${course.num}</td>
            <c:if test="${course.type == 0}">
                <td>
                    <select><option selected>选修</option><option>必修</option></select>
                </td>
            </c:if>
            <c:if test="${course.type == 1}">
                <td>
                    <select><option>选修</option><option selected>必修</option></select>
                </td>
            </c:if>
        </tr>
    </c:forEach>

</table>

<br>

<div align="center">
    <a href="addCourse.jsp"><input type="button" value="新增" ></a>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" value="删除" id="del">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" value="选课" id="hit">
<br><br>
</div>
<%--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--%>
<%--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--%>
<br><br>

<div align="center">
<c:if test="${type == 1 || type == 2}" var="_tag_1"><a href="CourseController?operation=all&tag=1">首页</a></c:if>
<c:if test="${type == 3 || type == 4}" var="_tag_2"><a href="CourseController?operation=all&tag=2">首页</a></c:if>
<c:if test="${!_tag_1 && !_tag_2}"><a href="CourseController?operation=all">首页</a></c:if>

<c:if test="${page.pageNo != 1}">
    <c:if test="${type == 1 || type == 2}" var="tag1"><a href="CourseController?operation=all&currentPage=${page.prevPage}&tag=1">上一页</a></c:if>
    <c:if test="${type == 3 || type == 4}" var="tag2"><a href="CourseController?operation=all&currentPage=${page.prevPage}&tag=2">上一页</a></c:if>
    <c:if test="${!tag1 && !tag2}"><a href="CourseController?operation=all&currentPage=${page.prevPage}">上一页</a></c:if>
</c:if>

<c:forEach begin="1" end="${page.totalPage}" var="i">
    <c:if test="${i == page.pageNo}" var="judge">${i}</c:if>
    <c:if test="${!judge}">
        <c:if test="${type == 1 || type == 2}" var="_tag1"><a href="CourseController?operation=all&currentPage=${i}&tag=1">${i}</a></c:if>
        <c:if test="${type == 3 || type == 4}" var="_tag2"><a href="CourseController?operation=all&currentPage=${i}&tag=2">${i}</a></c:if>
        <c:if test="${!_tag1 && !_tag2}"><a href="CourseController?operation=all&currentPage=${i}">${i}</a></c:if>
    </c:if>
</c:forEach>
<c:if test="${page.pageNo != page.totalPage}">
    <c:if test="${type == 1 || type == 2}" var="tag_1"><a href="CourseController?operation=all&currentPage=${page.nextPage}&tag=1">下一页</a></c:if>
    <c:if test="${type == 3 || type == 4}" var="tag_2"><a href="CourseController?operation=all&currentPage=${page.nextPage}&tag=2">下一页</a></c:if>
    <c:if test="${!tag_1 && !tag_2}"><a href="CourseController?operation=all&currentPage=${page.nextPage}">下一页</a></c:if>
</c:if>

<c:if test="${type == 1 || type == 2}" var="tag_1_"><a href="CourseController?operation=all&currentPage=${page.totalPage}&tag=1">尾页</a></c:if>
<c:if test="${type == 3 || type == 4}" var="tag_2_"><a href="CourseController?operation=all&currentPage=${page.totalPage}&tag=2">尾页</a></c:if>
<c:if test="${!tag_1_ && !tag_2_}"><a href="CourseController?operation=all&currentPage=${page.totalPage}">尾页</a></c:if><br>
    共查询到 ${page.totalSize} 条记录，当前第 ${page.pageNo} 页，共 ${page.totalPage} 页
</div>

<script>
    document.getElementById("del").onclick = function(){        //点击删除调用函数
        let result = "";
        let count = 0;
        let inputs = document.getElementsByTagName("input");
        for(let i = 0;i < inputs.length;i++){
            if(inputs[i].type === "checkbox" && inputs[i].checked){  //如果被选中
                result += inputs[i].value + ",";
                count++;
            }
        }
        if(count !== 0){
            result = result.substring(0,result.lastIndexOf(','));
            window.location.href= "CourseController?operation=del&tag="+result;
        }else{
            alert("请选中需要删除的课程！");
        }
    }

    document.getElementById("hit").onclick = function(){        //点击选课调用函数
        let result = "";
        let count = 0;
        let inputs = document.getElementsByTagName("input");
        for(let i = 0;i < inputs.length;i++){
            if(inputs[i].type === "checkbox" && inputs[i].checked){  //如果被选中
                result += inputs[i].value + ",";
                count++;
            }
        }
        if(count !== 0){
            result = result.substring(0,result.lastIndexOf(','));
            window.location.href= "CourseController?operation=hit&tag="+result;
        }else{
            alert("请选中需要选择的课程！");
        }
    }

    document.getElementById("cId").onclick = function (){
        window.location.href = "CourseController?operation=all";
    }

    document.getElementById("cName").onclick = function (){
        window.location.href= "CourseController?operation=all&tag=1&isClicked=1";
    }

    document.getElementById("cNum").onclick = function (){
        window.location.href= "CourseController?operation=all&tag=2&isClicked=1";
    }

</script>
</body>
</html>
